package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.DailyPaper;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.DailyPaperQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyPaperService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 日报管理
 */

@Controller
@RequestMapping("dailyPaper")
public class DailyPaperController {
    @Autowired
    private IDailyPaperService dailyPaperService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") DailyPaperQueryObject qo, Model model){
        model.addAttribute("pageInfo",dailyPaperService.query(qo));
        return "/dailyPaper/list";
    }

    /**
     * 新增和编辑
     * @param dailyPaper
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(DailyPaper dailyPaper, MultipartFile file) {
        //判断是否图片上传
        if (file != null && file.getSize()>0) {
            //上传图片到指定位置 获取路径
            String url = UploadUtil.uploadALiYun(file);
            //将url存到对象中
            dailyPaper.setCoverUrl(url);
        }
        dailyPaperService.saveOrUpdate(dailyPaper);
        return new JSONResult();
    }
}
